package third;


import org.apache.commons.beanutils.BeanUtils;
import second.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", value = "/register1")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受参数并封装成Bean对象
        //1第一种方式，手动获取参数,创建对象
        //request.getParameter();
        //2第二种方式,内省+反射  难度
        //3第三种方式，BeanUtils工具包
        request.setCharacterEncoding("utf-8");
        User user = new User();
        try {
            // 将Map数据封装到指定Javabean中, 一般用于将表单的所有数据封装到javabean
            BeanUtils.populate(user, request.getParameterMap());
            System.out.println(user.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
